﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class DayToDayManagement_ProductCommentManagement : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void Page_Init(object sender, EventArgs e)
    {
        // 确保每次回发都能动态创建GridView
        BindGridView("");
    }
    
    private string Condition
    {
        get
        {
            string condition = "";
            if (ViewState["Condition"] != null)
            {
                condition = ViewState["Condition"].ToString();
            }
            return condition;
        }
        set
        {
            ViewState["Condition"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //Check.CheckPermission("300100107");
        if (!IsPostBack)
        {
            AspNetPager1.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["RowCountPerPage"].ToString());
            BindGridView("");

        }

        Session["FCode"] = "300100107";
    }

    private void CreateGridViewColumns(DataSet dst)
    {
        grvComment.Columns.Clear();
        grvComment.AutoGenerateColumns = false;

        BoundField bfProductID = new BoundField();
        bfProductID.HeaderText = "商品代码";
        bfProductID.DataField = "商品代码";
        bfProductID.ReadOnly = true;

        BoundField bfProductName = new BoundField();
        bfProductName.HeaderText = "商品名称";
        bfProductName.DataField = "商品名称";
        bfProductName.ReadOnly = true;

        grvComment.Columns.Add(bfProductID);
        grvComment.Columns.Add(bfProductName);

        TemplateField tf;
        for (int i = 2; i < dst.Tables[0].Columns.Count; i++)
        {
            tf = new TemplateField();
            if (i + 1 == dst.Tables[0].Columns.Count)
            {
                tf.ItemTemplate = new GridViewLableTemplate("Label" + i.ToString(), dst.Tables[0].Columns[i].Caption,true);
                tf.HeaderText = "总评价次数";
            }
            else
            {
                if (i % 2 == 0)
                {
                    tf.ItemTemplate = new GridViewLinkButtonTemplate("HyperLink" + i.ToString(), dst.Tables[0].Columns[i].Caption, dst.Tables[0].Columns[0].Caption, dst.Tables[0].Columns[i].Caption);
                    tf.HeaderText = dst.Tables[0].Columns[i].Caption;
                }
                else
                {
                    tf.ItemTemplate = new GridViewLableTemplate("Label" + i.ToString(), dst.Tables[0].Columns[i].Caption,false);
                    tf.HeaderText = "百分比";
                }
            }
            grvComment.Columns.Add(tf);
        }
        
        
    }

    private void BindGridView(string sqlCondition)
    {
        int recordCount = 0;
        DataSet dst = new BLL.COMMODITY_EVAL().GetCommentStatistics(GetStartIndex(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize), AspNetPager1.PageSize, sqlCondition, out recordCount);
        CreateGridViewColumns(dst);
        grvComment.DataSource = dst;
        grvComment.DataBind();
        AspNetPager1.RecordCount = recordCount;
    }
   
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGridView(this.Condition);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        bool isFirstCondition = true; //标识是否是第一个where条件
        StringBuilder strCondition = new StringBuilder();
        strCondition.Append("where ");


        if (this.hidProduct.Value != "")
        {
            strCondition.Append("COMMODITY_EVAL.CE_CODE in (");
            strCondition.Append(this.hidProduct.Value);
            strCondition.Append(") ");
            isFirstCondition = false;
        }
        if (this.hidUser.Value != "")
        {
            if (isFirstCondition)
            {
                strCondition.Append("COMMODITY_EVAL.CE_USER in (");
                strCondition.Append(this.hidUser.Value);
                strCondition.Append(") ");
                isFirstCondition = false;
            }
            else
            {
                strCondition.Append("and COMMODITY_EVAL.CE_USER in (");
                strCondition.Append(this.hidUser.Value);
                strCondition.Append(") ");
            }
        }
        try
        {
            if (txtEndDate.Text != "" && txtStartDate.Text != "")
            {
                if (isFirstCondition)
                {
                    strCondition.Append("COMMODITY_EVAL.CE_TIME>'");
                    strCondition.Append(DateTime.Parse(txtStartDate.Text));
                    strCondition.Append("' and COMMODITY_EVAL.CE_TIME<'");
                    strCondition.Append(DateTime.Parse(txtEndDate.Text));
                    strCondition.Append("' ");
                    isFirstCondition = false;
                }
                else
                {
                    strCondition.Append("and COMMODITY_EVAL.CE_TIME>'");
                    strCondition.Append(DateTime.Parse(txtStartDate.Text));
                    strCondition.Append("' and COMMODITY_EVAL.CE_TIME<'");
                    strCondition.Append(DateTime.Parse(txtEndDate.Text));
                    strCondition.Append("' ");
                }
            }
            if (!isFirstCondition)
            {
                this.Condition = strCondition.ToString();
            }
            else
            {
                this.Condition = "";
            }
            BindGridView(this.Condition);
        }
        catch (FormatException EX)
        {
            Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('输入的日期格式有误，请重新输入。')</script>");
        }
        catch
        {
            Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('查询是出现错误，请重新查询。')</script>");
        }
    }
    private int GetStartIndex(int pageIndex, int pageSize)
    {
        return 1 + (pageIndex - 1) * pageSize;
    }
}
